﻿using jlynet.core.Library;
using jlynet.core.notebook.model;
using jlynet.core.notebook.util;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SQLite;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace jlynet.core.notebook.myform
{
    public partial class CreatePasswordBoxForm : Form
    {
        public CreatePasswordBoxForm()
        {
            InitializeComponent();
            this.MinimizeBox = false;  //去掉最小化
            this.MaximizeBox = false;  //去掉最大化
            DoBindDataSource();
            comboBox_problemSelection.DropDownStyle = ComboBoxStyle.DropDownList;
            comboBox_problemSelection.Enabled = false;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            // "保存为"对话框
            SaveFileDialog dialog = new SaveFileDialog();
            dialog.Filter = Constant.DB_FileSuffix;
            //设置默认打开路径(绝对路径)
            dialog.InitialDirectory = @Library.SysHelper.GetStartupPath();
            // 显示对话框
            if (dialog.ShowDialog() == DialogResult.OK) {
                // 文件名
                string fileName = dialog.FileName;
                textBox_createFilePath.Text = fileName;
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            //Console.WriteLine("IsNullOrWhiteSpace: "+String.IsNullOrWhiteSpace(textBox_confirmPassword.Text));
            //Console.WriteLine("IsNullOrEmpty: " + String.IsNullOrEmpty(textBox_confirmPassword.Text));
            //Console.WriteLine("=========="+!StringHelper.IsNullOrEmpty(textBox_password.Text));

            if (!StringHelper.IsNullOrEmpty(this.textBox_createFilePath.Text))
            {
                if (!StringHelper.IsNullOrEmpty(this.textBox_password.Text) &&
                !StringHelper.IsNullOrEmpty(this.textBox_confirmPassword.Text))
                {
                    if (this.textBox_password.Text.Equals(this.textBox_confirmPassword.Text))
                    {
                        //用户现在设置问题和找回密码
                        if (checkBox1.CheckState == CheckState.Checked && StringHelper.IsNullOrEmpty(textBox_answer.Text))
                        {
                            MessageBox.Show("请设置提示问题答案！");
                            return;
                        }
                            //设置数据库路径
                            SQLiteConfigHelper.DatabaseFile = textBox_createFilePath.Text;
                            if (MySQLiteHelper.CreateDB())
                        {
                            //MessageBox.Show("成功创建库");
                            DBInitialization.initialization();
                            string password = DESEncrypt.Encrypt(this.textBox_password.Text, Constant.DES_KEY);
                            string problem = "";
                            string answer = "";
                            //用户现在设置问题和找回密码
                            if (checkBox1.CheckState == CheckState.Checked){
                                System.Data.DataRowView dataRowView = (DataRowView)this.comboBox_problemSelection.SelectedItem;
                                DataRow dataRow = dataRowView.Row;
                                problem = (string) dataRow.ItemArray[1];
                                //string problem = this.comboBox_problemSelection.SelectedName;
                                answer = DESEncrypt.Encrypt(this.textBox_answer.Text, Constant.DES_KEY); 
                            }
                            DBInitialization.insert_userInfoTable(password, problem, answer);

                            //写入配置文件
                            INIFileInitialization ini = new INIFileInitialization();
                            if (File.Exists(INIFileInitialization.INIPath))
                            {
                                INIModel model = new INIModel();
                                model.node = "Options";
                                List<keyValue> keyValues = new List<keyValue>();
                                keyValue keyValue02 = new keyValue();
                                keyValue02.name = "LastFile";
                                keyValue02.value = SQLiteConfigHelper.DatabaseFile;
                                keyValues.Add(keyValue02);
                                model.keyValue = keyValues;
                                ini.initialization(model);
                            }
                            else
                            {
                                ini.initialization();
                            }

                            
                            this.DialogResult = DialogResult.OK;
                            this.Dispose();
                            this.Close();
                        }
                        else{
                            MessageBox.Show("密码箱创建失败！");
                            return;
                        }
                    }else {
                        MessageBox.Show("二次确认密码不正确！");
                        return;
                    }
                }else{
                    MessageBox.Show("登录密码不能为空！");
                    return;
                }
            }else{
                MessageBox.Show("创建密码箱文件地址丢失！");
                return;
            }
        }

        

        private void checkBox1_Click(object sender, EventArgs e)
        {
            if (checkBox1.CheckState == CheckState.Checked)
            {
                comboBox_problemSelection.Enabled = true;
                textBox_answer.ReadOnly = false;
                //comboBox_problemSelection.Items.Add("");
                // MessageBox.Show("checkbox1 is checked\n" + checkBox1.Text);
            }
            else if (checkBox1.CheckState == CheckState.Unchecked)
            {
                comboBox_problemSelection.Enabled = false;
                textBox_answer.ReadOnly = true;
                //MessageBox.Show("checkbox1 is Unchecked\n" + checkBox1.Text);
            }
            else
            {
               // MessageBox.Show("checkBox1 控件处于不确定状态");
            }
        }

        /// <summary>
        /// 初始化提示问题数据
        /// </summary>
        private void DoBindDataSource()
        {
            //构造数据源（或从数据库中查询）  
            DataTable ADt = new DataTable();
            DataColumn ADC1 = new DataColumn("F_ID", typeof(int));
            DataColumn ADC2 = new DataColumn("F_Name", typeof(string));
            ADt.Columns.Add(ADC1);
            ADt.Columns.Add(ADC2);

            DataRow ADR_1 = ADt.NewRow();
            ADR_1[0] = 1;
            ADR_1[1] = "你姐姐叫什么名字？";
            ADt.Rows.Add(ADR_1);

            DataRow ADR_2 = ADt.NewRow();
            ADR_2[0] = 2;
            ADR_2[1] = "你的偶像是谁？";
            ADt.Rows.Add(ADR_2);

            DataRow ADR_3 = ADt.NewRow();
            ADR_3[0] =3;
            ADR_3[1] = "你喜欢谁？";
            ADt.Rows.Add(ADR_3);

            DataRow ADR_4 = ADt.NewRow();
            ADR_4[0] = 4;
            ADR_4[1] = "你的高中英语老师叫什么名字？";
            ADt.Rows.Add(ADR_4);

            DataRow ADR_5 = ADt.NewRow();
            ADR_5[0] = 5;
            ADR_5[1] = "你的高中数学老师叫什么名字？";
            ADt.Rows.Add(ADR_5);

            DataRow ADR_6 = ADt.NewRow();
            ADR_6[0] = 6;
            ADR_6[1] = "你的宠物叫什么名字？";
            ADt.Rows.Add(ADR_6);



            //进行绑定  
            comboBox_problemSelection.DisplayMember = "F_Name";//控件显示的列名  
            comboBox_problemSelection.ValueMember = "F_ID";//控件值的列名  
            comboBox_problemSelection.DataSource = ADt;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            this.Close();
            this.Dispose();
        }
    }
}
